/**************************************************************************
** Yabet is bloging editor tool that supports many blog's formats. It is very usefull for crossposting, allows easily to
** make posts to your blogs.
**
** Copyright (C) 2010 Valery Konchin <eeexception@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
**     it under the terms of the GNU General Public License as published by
**     the Free Software Foundation, either version 3 of the License, or
**     (at your option) any later version.
**
**     This program is distributed in the hope that it will be useful,
**     but WITHOUT ANY WARRANTY; without even the implied warranty of
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**     GNU General Public License for more details.
**
**     You should have received a copy of the GNU General Public License
**     along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#include "googletranslater.h"
#include <QNetworkAccessManager>

const YabetLogHandler* GoogleTranslater::log = new YabetLogHandler("GoogleTranslater");

GoogleTranslater::GoogleTranslater()
{
    langShort
        <<"sq" <<"ar" <<"bg" <<"ca"
        <<"zh-CN"  <<"zh-TW"  <<"hr"
        <<"cs" <<"da" <<"nl" <<"en"
        <<"et" <<"tl" <<"fi" <<"fr"
        <<"gl" <<"de" <<"el" <<"iw"
        <<"hi" <<"hu" <<"id" <<"it"
        <<"ja" <<"ko" <<"lv" <<"lt"
        <<"mt" <<"no" <<"pl" <<"pt"
        <<"ro" <<"ru" <<"sr" <<"sk"
        <<"sl" <<"es" <<"sv" <<"th"
        <<"tr" <<"uk" <<"vi";
    langFull
        <<"Albanian" <<"Arabic" <<"Bulgarian" <<"Catalan"
        <<"Chinese (Simplified)"  <<"Chinese (Traditional)"  <<"Croatian"
        <<"Czech" <<"Danish" <<"Dutch" <<"English"
        <<"Estonian" <<"Filipino" <<"Finnish" <<"French"
        <<"Galician" <<"German" <<"Greek" <<"Hebrew"
        <<"Hindi" <<"Hungarian" <<"Indonesian" <<"Italian"
        <<"Japanese" <<"Korean" <<"Latvian" <<"Lithuanian"
        <<"Maltese" <<"Norwegian" <<"Polish" <<"Portuguese"
        <<"Romanian" <<"Russian" <<"Serbian" <<"Slovak"
        <<"Slovenian" <<"Spanish" <<"Swedish" <<"Thai"
        <<"Turkish" <<"Ukrainian" <<"Vietnamese";
}

QString GoogleTranslater::translate(QString text, QString fromLang, QString toLang) {
    QNetworkAccessManager* manager = new QNetworkAccessManager();

    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

    QString url = "http://translate.google.com/translate_a/t?sl=%1&tl=%2&client=t&oe=UTF-8";
    QString fromShort = langShort.at(langFull.indexOf(fromLang));
    QString toShort = langShort.at(langFull.indexOf(toLang));

    log->debugMsg("From language " + fromLang + " [" + fromShort + "]");
    log->debugMsg("To language " + toLang + " [" + toShort + "]");

    QNetworkRequest request(QUrl(url.arg(fromShort).arg(toShort)));

    QByteArray data("text=");
    data.append(text.toUtf8());

    request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(data.length()));
    request.setRawHeader("Host", "www.google.com");
    request.setRawHeader("Accept-Encoding", "deflate");
    request.setRawHeader("Accept-Charset", "utf-8");
    request.setRawHeader("Connection", "Close");

    manager->post(request, data);

    loop.exec();

    log->debugMsg("Translate: " + translateResult);

    return translateResult;
}

QStringList GoogleTranslater::getLanguagesList() {
    return langFull;
}

void GoogleTranslater::replyFinished(QNetworkReply *reply) {
    if(reply->error() != QNetworkReply::NoError) {
        log->warningMsg(reply->errorString());

        emit wasError(reply->errorString());

        loop.exit();

        return;
    }

    QString replyString = replyString.fromUtf8(reply->readAll());

    log->debugMsg("Full reply: " + replyString);

    QString fromString = "trans\":\"";
    QString toString = "\",\"orig\":";

    int from = replyString.indexOf(fromString);
    int to = replyString.indexOf(toString);

    translateResult = replyString.mid(from + fromString.length(), to - from -fromString.length());

    loop.exit();
}
